<?php

namespace api\channel\jutuike;

use api\channel\jutuike\Base;
use app\model\BaseSupplier;
use Carbon\Carbon;
use think\Exception;

/**
 * 聚推客联盟(权益的)
 * 接口文档：https://www.jutuike.com/doc/83
 */
class Coupon extends Base
{

    public function __construct()
    {
        parent::__construct(PRODUCT_COUPON_SERV);
    }

    /**
     * 会员卡券分类接口
     *
     * @link https://www.jutuike.com/doc/84
     */
    public function category()
    {
        $url = $this->baseUrl . '/wap/category';
        $data = [
            'apikey' => $this->apikey,
            'type' => 1,
        ];
        $res = curlPost($url, $data);
        $res = json_decode($res, true);
        return $res['data'] ?? [];
    }

    /**
     * 会员卡券产品列表
     *
     * @link https://www.jutuike.com/doc/85
     * @param $cid 分类id，必传
     * @param $page 页码，默认 1
     * @param $pageSize 每页最多返回产品数量，默认 100
     */
    public function productList(int $cid, int $page = 1, int $pageSize = 100)
    {
        $url = $this->baseUrl . '/wap/product_list';
        $data = [
            'apikey' => $this->apikey,
            'cid' => $cid,
            'page' => $page,
            'pageSize' => $pageSize,
        ];
        $res = curlPost($url, $data);
        $res = json_decode($res, true);
        return $res['data'] ?? [];
    }

    /**
     * 会员卡券产品转链接口
     *
     * @link https://www.jutuike.com/doc/83
     *
     * @param $productId 产品id
     * @param $args      其他需要的参数, agent_id, user_id
     *
     * @return $url      产品短链接
     */
    public function productUrl(int $productId, array $args = [])
    {
        $mobile = $args['mobile']??'';

        $sid = $this->getSid($args['agent_id'], $args['user_id'],$args['channel_id']??'');

        $url = $this->baseUrl . '/wap/act';
        $data = [
            'apikey' => $this->apikey,
            'sid' => $sid,
            'query' => 'pages/digital/index',
            'product_id' => $productId,
            'showMenu' => true,
            'mobile' => $mobile,
        ];


        $res = curlPost($url, $data);
        $res = json_decode($res, true);
        return $url = $res['data']['short_url'] ?? '';
    }

    /**
     * 聚推客联盟统一订单数据接口
     *
     * @link https://www.jutuike.com/doc/37
     *
     * @param $start_time   开始时间，示例：2021-06-05 21:41:52
     * @param $end_time     结束时间，示例：2021-06-05 21:41:52
     * @param $query_type   排序类型：1按支付时间 2按更新时间 3创建时间，默认3
     * @param $act_id       活动id。会员卡券 act_id = 50
     * @param $page         页码。默认1
     * @param $page_size    每页显示多少。默认20 最大100
     *
     * @return
     * Array
        (
            [total] => 1
            [per_page] => 100
            [current_page] => 1
            [last_page] => 1
            [data] => Array
                (
                    [0] => Array
                        (
                            [act_name] => 会员卡券
                            [act_id] => 50
                            [sid] => 1234
                            [jtk_share_rate] => 0.050
                            [jtk_share_fee] => 0.94
                            [order_sn] => 2022041298100504
                            [order_title] => 腾讯视频VIP1个月
                            [create_time] => 2022-04-12 09:49:06
                            [pay_time] => 2022-04-12 09:49:06
                            [modified_time] => 2022-04-12 09:49:11
                            [order_price] => 18.70
                            [pay_price] => 18.70
                            [status] => 3
                            [status_desc] => 已结算
                            [sub_share_rate] => 0.000
                            [sub_share_fee] => 0.00
                            [pub_id] => 24364
                            [extension] =>
                            [icon] => https://img.jutuike.com/taokeout/icon/digital.png
                        )

                )

        )
     */
    public function orders($params)
    {
        $page = $params['page']??1;
        $url = $this->baseUrl . '/union/orders';

        $filter = [
//            'start_time' => Carbon::now()->subSeconds(3600*24*360)->format('Y-m-d H:i:s'),
            'start_time' => Carbon::now()->subHours(2)->format('Y-m-d H:i:s'),
            'end_time' => date('Y-m-d H:i:s'), // 结束时间
            'query_type' => 2, // 排序类型：1按支付时间 2按更新时间 3创建时间，默认3
            'act_id' => 50, // 活动id。会员卡券
            'page' => $page, // 页码
            'page_size' => 100, // 每页显示多少条
        ];


        $data = [
            'apikey' => $this->apikey,
        ];

        $data = handleParameters($data,$filter,$params);

        $res = curlPost($url, $data);
        $res = json_decode($res, true);
        return $res['data'] ?? [];
    }


    /**
     * 返回供应商id
     */
    public function getId(): int
    {
        return $this->supplierInfo['id'];
    }


}
